package com.trade.module.daq.controller;

import com.trade.common.utils.*;
import com.trade.module.base.controller.BaseController;
import com.trade.module.base.entity.SystemLog;
import com.trade.module.base.service.SystemLogService;
import com.trade.module.base.vo.UserVo;
import com.trade.module.daq.entity.SystemInfo;
import com.trade.module.daq.service.SystemInfoService;
import com.trade.module.database.meta.TableMetaData;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Desc: 数据源系统控制器
 * @Version: v1.0 2017/12/8 15:02
 * @Author: zfb
 * @JDK Version: 1.8.0_51
 */
@Log4j
@Controller
@RequestMapping(value = "/daq/systemInfo")
public class SystemInfoController extends BaseController{

    @Autowired
    private SystemInfoService systemInfoService;

    @Autowired
    private SystemLogService systemLogService;

    /**
     * 新增数据源
     * @param systemInfo 系统对象
     * @param request    请求
     * @return ResponseBean
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    private ResponseBean save(@RequestBody SystemInfo systemInfo, HttpServletRequest request) {
        //获取当前操作人信息
        UserVo currentUser = getCurrentUser();
        String currentUserId = currentUser!=null?currentUser.getId():"";
        systemInfo.setCreaterId(currentUserId);
        systemInfo.setModifyId(currentUserId);
        SystemInfo systemInfoPo = systemInfoService.save(systemInfo);
        //打印日志
        if (XaUtil.isNotEmpty(systemInfoPo)) {
            SystemLog sysLog = LogUtil.buildeLogBean(SystemInfo.class, LogUtil.LogLevel.info, LogUtil.CRUDType.create, LogUtil.OperatRsult.success
                    , currentUserId, (currentUser != null) ? currentUser.getAccount() : "", systemInfo.getId(), NetUtil.getIp(request), "添加成功");
            systemLogService.save(sysLog);
            log.info(LogUtil.getLogJsonFromBean(sysLog).toString());
            return new ResponseBean(ResponseUtil.MODIFY_OK_201);
        } else {
            SystemLog sysLog = LogUtil.buildeLogBean(SystemInfo.class, LogUtil.LogLevel.error, LogUtil.CRUDType.create, LogUtil.OperatRsult.failure
                    , currentUserId, (currentUser != null) ? currentUser.getAccount() : "", "", NetUtil.getIp(request), "添加失败");
            systemLogService.save(sysLog);
            log.error(LogUtil.getLogJsonFromBean(sysLog).toString());
            return new ResponseBean(ResponseUtil.VERIFY_ERROR_422, "失败");
        }
    }

    /**
     * 根据系统id检测表同步（指的是数据源表变换时目标源可以同步）
     *
     * @param systemId 系统id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "synTableList/{systemId}", method = RequestMethod.GET)
    public ResponseBean<List<TableMetaData>> getSynTableList(@PathVariable String systemId) throws Exception {
        return systemInfoService.getSynTableList(systemId);
    }
}
